package com.ruoyi.system.controller;

import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.LongVideo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.ShortVideo;
import com.ruoyi.system.service.IShortVideoService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 短视频管理Controller
 *
 * @author ruoyi
 * @date 2024-10-29
 */
@RestController
@RequestMapping("/short")
public class ShortVideoController extends BaseController
{
    @Autowired
    private IShortVideoService shortVideoService;

    /**
     * 查询短视频管理列表
     */
    @RequiresPermissions("system:short:list")
    @GetMapping("/list")
    public TableDataInfo list(ShortVideo shortVideo)
    {
        startPage();
        List<ShortVideo> list = shortVideoService.selectShortVideoList(shortVideo);
        return getDataTable(list);
    }

    /**
     * 导出短视频管理列表
     */
    @RequiresPermissions("system:short:export")
    @Log(title = "短视频管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ShortVideo shortVideo)
    {
        List<ShortVideo> list = shortVideoService.selectShortVideoList(shortVideo);
        ExcelUtil<ShortVideo> util = new ExcelUtil<ShortVideo>(ShortVideo.class);
        util.exportExcel(response, list, "短视频管理数据");
    }

    /**
     * 获取短视频管理详细信息
     */
    @RequiresPermissions("system:short:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(shortVideoService.selectShortVideoById(id));
    }

    /**
     * 新增短视频管理
     */
    @RequiresPermissions("system:short:add")
    @Log(title = "短视频管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShortVideo shortVideo)
    {
        shortVideo.setMv("播放");
        shortVideo.setUproot("root");
        shortVideo.setStart(1);
        shortVideo.setAdmin("周正");
        shortVideo.setTime(new Date());
        shortVideo.setEnd(new Date());
        shortVideo.setStatus(1);
        return toAjax(shortVideoService.insertShortVideo(shortVideo));
    }

    /**
     * 修改短视频管理
     */
    @RequiresPermissions("system:short:edit")
    @Log(title = "短视频管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShortVideo shortVideo)
    {
        if (shortVideo.getStart()==1){
            return toAjax(shortVideoService.updateShortVideo(shortVideo));
        }else {
            shortVideo.setStart(1);
            return toAjax(shortVideoService.updateShortVideo(shortVideo));
        }
    }

    /**
     * 删除短视频管理
     */
    @RequiresPermissions("system:short:remove")
    @Log(title = "短视频管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(shortVideoService.deleteShortVideoByIds(ids));
    }


    @GetMapping(value = "/getInfoByLongId/{longId}")
    public AjaxResult getInfoByLongId(@PathVariable("id") Integer id){
        List<ShortVideo> longVideos = shortVideoService.selectLongVideoById(id);
        return success(longVideos);
    }

    /**
     * 导入会员订单管理数据
     */
    @RequiresPermissions("system:short:daoru")
    @Log(title = "会员订单管理", businessType = BusinessType.IMPORT)
    @PostMapping("/daoru")
    public AjaxResult importData(@RequestParam("file") MultipartFile file) throws IOException
    {
        ExcelUtil<ShortVideo> util = new ExcelUtil<ShortVideo>(ShortVideo.class);
        List<ShortVideo> financeMemberList = util.importExcel(file.getInputStream());
        shortVideoService.importFinanceMember(financeMemberList);
        return success();
    }
}
